<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input class="input" type="text">
    <button class="btn">提交</button>
</body>
<script type="text/javascript">
    var input = document.querySelector('.input')
    var btn = document.querySelector('.btn')
    var arr = [1, 2, 3]

    function insertSort(arr) {
        var len = arr.length
        var preIndex, current
        for (let i = 1; i < len; i++) {
            preIndex = i - 1
            current = arr[i]
            while (preIndex >= 0 && arr[preIndex] > current) {
                arr[preIndex + 1] = arr[preIndex]
                preIndex--
            }
            arr[preIndex + 1] = current
        }
        return arr
    }

    function quickSort(array, low, high) {
        if (low >= high) {
            return array;
        }
        var key = array[low];
        var start = low;
        var end = high;
        while (start < end) {
            while (start < end && key <= array[end]) end--;
            while (start < end && array[start] <= key) start++;
            if (start < end) {
                var tmp = array[start];
                array[start] = array[end];
                array[end] = tmp;
            }
        }
        array[low] = array[start];
        array[start] = key;
        quickSort(array, low, start - 1);
        quickSort(array, start + 1, high);
        return array;
    }

    function sum(a, b) {
        return Number(a) + Number(b)
    }

    function bigSum(a, b) {
        let arrayA = a.split('')
        let arrayB = b.split('')
        let carry = 0
        let res = []
        let distance = arrayA.length - arrayB.length
        if (distance > 0) {
            for (let i = 0; i <= distance; i++) {
                arrayB.unshift('0')
            }
            arrayA.unshift('0')
        } else {
            for (let i = 0; i <= distance; i++) {
                arrayA.unshift('0')
            }
            arrayB.unshift('0')
        }
        for (let i = arrayA.length - 1; i >= 0; i--) {
            let sum = Number(arrayA[i]) + Number(arrayB[i]) + Number(carry)
            carry = sum >= 10 ? 1 : 0
            sum = sum >= 10 ? sum % 10 : sum
            res.unshift(sum)
        }
        return res.join('').replace(/^0/, '')
    }
    var twoSum = function (n) {
        var res = []
        var dp = []
        for (let i = 0; i < n + 1; i++) {
            dp[i] = []
            for (let j = 0; j < 6 * n + 1; j++) {
                dp[i][j] = 0
            }
        }
        dp[0][0] = 1
        for (let i = 1; i <= n; i++) {
            for (let j = i; j <= 6 * i; j++) {
                for (let k = 1; k <= Math.min(j, 6); k++) {
                    dp[i][j] += dp[i - 1][j - k]
                }
            }
        }
        var sum = 0
        for (let i = n; i <= 6 * n; i++) {
            sum += dp[n][i]
        }
        for (let i = n; i <= 6 * n; i++) {
            res.push(dp[n][i] / sum)
        }
        return dp
    };

    function strToInt(str) {
        if (str[0] != (/[A-z]/)) {
            console.log(str)
            return 0
        }
        str = str.replace(/[^0-9+-]/g, '')
        if (str.length == 0) {
            return 0
        }
        var k = 0
        var res = 0
        var isMinus = false
        if (str[0] == '+') {
            k++
        } else if (str[0] == '-') {
            isMinus = true
            k++
        }
        for (let i = k; i < str.length; i++) {
            res = res * 10 + Number(str[i])
        }
        if (isMinus == true) {
            res = res * (-1)
        }
        if (res > Math.pow(2, 31) - 1) {
            return Math.pow(2, 31) - 1
        } else if (res < -Math.pow(2, 31)) {
            return -Math.pow(2, 31)
        }
        return res
    };
    btn.onclick = function () {

        var Array = input.value
        // var out = bigSum(Array[0], Array[1])
        console.log(strToInt(Array))
    }
</script>

</html>